﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class profile : System.Web.UI.Page
{

    void generateGroupList()
    {
        //DropDownList_usergroup.Items.Add();
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        CGeneral.CheckLogin(Request, Session);

        //统一隐藏 选择性显示项
        profile_usergroup.Visible = false;
        avatar.Visible = false;
        profile_info.Visible = false;


        MemDisplay m=null;
        Permission per = new Permission(int.Parse(Session["gid"].ToString()));

        //判断是否是admin在修改别人的信息

        int tarGid = -1;//修改后GID
        if (Request.QueryString["uid"] != null)//管理模式
        {
            //检查session权限是否是admin，不是则跳转错误
            if (per.awmag == 0)
                CGeneral.GoToErrorPage(Response, "custom", "YOU NEED MORE POWER");

            try
            {
                m = new MemDisplay(int.Parse(Request.QueryString["uid"].ToString()));
                
            }
            catch
            {
                Response.Redirect("Index.aspx");
            }

            //重设右侧导航
            profile_right_avatar_link.HRef = "profile.aspx?type=avatar&uid=" + Request.QueryString["uid"];
            profile_right_info_link.HRef = "profile.aspx?type=info&uid="+Request.QueryString["uid"];

            //显示用户组下拉
            profile_usergroup.Visible = true;      
        }
        else//查看自己模式
        {
            try
            {
                m = new MemDisplay(int.Parse(Session["uid"].ToString()));
            }
            catch
            {
                Response.Redirect("Index.aspx");
            }
        }
        //uid错误
        if (m.isExist == 0)
            CGeneral.GoToErrorPage(Response, "custom", "参数错误", "不存在该用户");

        if (Request.QueryString["type"]==null)
        {
            CGeneral.GoToErrorPage(Response, "ParamWrong");
        }
        string type=Request.QueryString["type"];


        

        if (type == "avatar")
        {
            avatar.Visible = true;
            //上传头像按钮
            if (IsPostBack)
            {
                if (FileUpload.HasFile)
                {
                    string extend = System.IO.Path.GetExtension(FileUpload.FileName);
                    extend=extend.ToLower();
                    if (extend==".jpg" ||extend==".bmp" ||extend==".png" ||extend==".gif")
                    {
                        string filename = Server.MapPath("avatar") + "\\ori" + m.uid + extend;
                        FileUpload.SaveAs(filename);
                        m.UpdateAvatar(filename,"Big");
                        m.UpdateAvatar(filename, "Small");
                        m.hasAvatar = 1;
                        SaveData.UpdateMember(m);
                        System.IO.File.Delete(filename);
                        Response.Redirect(Request.UrlReferrer.ToString());
                    }
                }
            }
            //更新头像
            if (m.hasAvatar == 0)
            {
                Image_avatar.ImageUrl = "./avatar/noavatar_middle.gif";
                Image_avatar_small.ImageUrl = "./avatar/noavatar_small.gif";
            }
            else 
            {
                Image_avatar.ImageUrl = "./avatar/" + m.uid + "-big.png";
                Image_avatar_small.ImageUrl = "./avatar/" + m.uid + "-small.png";
            }

        }
        else if(type=="info")
        {
            profile_info.Visible = true;



            if (IsPostBack)
            {
                //更新信息
                bool finish = true;
                if (Check.IsNicknameLegal(Text_nickname.Value))
                    m.nickname = Text_nickname.Value;
                else
                {
                    this.Literal_nick.InnerText = "昵称违法";
                }
                string pwd1=Text_newpassword.Value;
                string pwd2=Text_newpassword2.Value;
                if(!string.IsNullOrEmpty(pwd1)&&!string.IsNullOrEmpty(pwd2))
                {
                    if (!Check.IsPasswordLegal(pwd1))
                        this.Literal_password.InnerText = "密码格式不正确";
                    else if (pwd1 != pwd2)
                        this.Literal_password2.InnerText = "两次密码不一致";
                    else
                        m.password = ConvertData.ToMD5(pwd1);

                }

                if (Check.CheckBirthFormat(Text_birthday.Value))
                {
                    m.birthday = Text_birthday.Value;
                    m.SetBirth();
                }
                else
                {
                    this.Literal_birth.InnerText = "生日格式错误";
                }

                if (Check.CheckMailLegal(Text_mail.Value))
                    m.email = Text_mail.Value;
                else
                {
                    this.Literal_mail.InnerText = "邮箱格式错误";
                }

                m.gender = Text_gender.Value;

                if (Check.CheckQQLegal(Text_qq.Value))
                    m.QQ = Text_qq.Value;
                else
                {
                    this.Literal_qq.InnerText = "QQ格式错误";
                }
                m.department = Text_department.Value;

                


                //设置用户组
                if (per.awmag == 1)
                {
                    int newGid = int.Parse(DropDownList_usergroup.SelectedValue.ToString());
                    m.gid = newGid;
                }


                if (finish)
                    m.Update();
                
            }

            else
            {
                Text_nickname.Value = m.nickname;
                Text_birthday.Value = m.birthday;
                Text_gender.Value = m.gender;
                Text_qq.Value = m.QQ;
                Text_mail.Value = m.email;
                Text_grade.Value = "08";
                Text_department.Value = m.department;

                //设置用户组
                //
            }
            //显示用户组下拉
            GetData gd = new GetData();
            List<groups> gr = gd.GetAllGroup();
            int index = -1;
            tarGid = m.gid;
            for (int i = 0; i < gr.Count(); i++)
            {
                if (gr[i].gid == tarGid)
                {
                    index = i;
                    break;
                }
            }
            DropDownList_usergroup.DataValueField = "gid";
            DropDownList_usergroup.DataTextField = "Name";
            DropDownList_usergroup.DataSource = gr;
            DropDownList_usergroup.DataBind();
            DropDownList_usergroup.SelectedIndex = index;
        }
    }



}